package es.unileon.is.finalproyect.userinterface;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

import es.unileon.is.finalproyect.temporizer.Temporizer;
import es.unileon.is.finalproyect.temporizer.TimeLabel;
import es.unileon.is.finalproyect.texts.Text;
import es.unileon.is.finalproyect.users.User;

public class TextReaderInterface extends JDialog {

	private Text _text;
	private User _user;
	private JButton _okButton;
	/**
	 * Create the frame.
	 */
	public TextReaderInterface(Text t, User u, JFrame parent, boolean modal) {
		super(parent, modal);
		_text = t;
		_user = u;
		setPreferredSize(new Dimension(800,600));
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		getContentPane().setLayout(new BorderLayout());
		
		//*****modificaciones para JAVAHELP*******
		JMenuBar menuBar = new JMenuBar();
		JMenu menuHelp = new JMenu("Help");
		JMenuItem help = new JMenuItem("Show help");
		menuBar.add(Box.createHorizontalGlue());
		menuBar.add(menuHelp);
		menuHelp.add(help);
		setJMenuBar(menuBar);
		//metodo para dar la accion a help
		ViewHelp.showHelp(help, getContentPane(), "ventana_lectura-texto");
		//*****************************************
		
		JTextArea textAreaL = new JTextArea();
		JScrollPane scrollT = new JScrollPane(textAreaL);
		textAreaL.setText(new String(t.getData()));
		getContentPane().add(scrollT,BorderLayout.CENTER);
		
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
		getContentPane().add(buttonPane,BorderLayout.SOUTH);
		
		_okButton = new JButton("Finish");
		
		final Temporizer clock = new Temporizer(_text.getSecondsToRead(), _okButton);
		TimeLabel timeLabel = new TimeLabel(clock);
		buttonPane.add(Box.createHorizontalGlue());
		buttonPane.add(timeLabel);
		buttonPane.add(Box.createRigidArea(new Dimension(20,0)));
		buttonPane.add(_okButton);
		_okButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				clock.setDone();
				dispose();
				new ResolveTextInterface(_text, _user, (JFrame) getParent(), isModal()).setVisible(true);
			}
		});
		
		pack();
		clock.start();
	}

}
